package io.renren.modules.basedata.service.impl;

import cn.com.taiji.common.manager.net.http.binclient.ApiRequestException;
import cn.com.taiji.sdk.comm.ETCCommHelper;
import cn.com.taiji.sdk.model.comm.protocol.sdts.expand.ServiceAreaUploadRequest;
import cn.com.taiji.sdk.model.comm.protocol.sdts.expand.ServiceAreaUploadResponse;
import io.renren.modules.basedata.dao.ServiceAreaUploadDao;
import io.renren.modules.basedata.entity.tbl_ServiceArea;
import io.renren.modules.basedata.service.ServiceAreaUploadService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.List;

@Service
public class ServiceAreaUploadServiceImpl implements ServiceAreaUploadService {

    private static final Logger log = LoggerFactory.getLogger(ServiceAreaUploadServiceImpl.class);

    @Autowired
    private ServiceAreaUploadDao serviceAreaUploadDao;

    @Override
    public String ServiceAreaUpload() throws Exception {
        log.info("开始获取服务区信息！");
        int operResult = 0;
        List<tbl_ServiceArea> list = null;
        tbl_ServiceArea serviceArea = null;
        list = getServiceAreaList();//获取收费车道信息
        if(list!=null&&list.size()>0){
            for(int i=0;i<list.size();i++){
                serviceArea = list.get(i);
                //服务区信息上传及变更接口
                ServiceAreaUploadRequest request = new ServiceAreaUploadRequest();
                request.setId(serviceArea.getId());
                request.setName(serviceArea.getName());
                request.setGasStationId(serviceArea.getGasStationId());
                request.setServiceAreaId(serviceArea.getServiceAreaId());
                request.setLat(""+serviceArea.getLat());
                request.setLng(""+serviceArea.getLng());
                request.setProvinceNum(serviceArea.getClearingId().toString());
                request.setStartTime(serviceArea.getStartTime());
                request.setEndTime(serviceArea.getEndTime());
                request.setOperation(serviceArea.getFlag());
                request.setStakeNum(serviceArea.getStackNum());

                //获取发送文件名
                String fileName = ETCCommHelper.getFileName(request, "65");
                try{
                    //指定响应的协议模型,调用upload发送数据
                    ServiceAreaUploadResponse response = ETCCommHelper.sdtsUpload(fileName, request, ServiceAreaUploadResponse.class);
                    log.info("上传服务区信息成功,服务器返回:"+response.toJson());
                    if(response!=null&&response.getInfo().equals("成功！")){
                        //更新上传标志
                        if(serviceAreaUploadDao.updateServiceArea(serviceArea.getId())==1){
                            log.info("更新服务区信息表上传标志成功!");
                            operResult = 1;
                        }else{
                            log.info("更新服务区信息表表上传标志成功!");
                        }
                    }else{
                        log.info("上传服务区信息失败!失败原因:"+response.getInfo());
                    }
                }catch(IOException e){
                    log.error("上传服务区信息失败！错误信息:"+e.getMessage());
                    continue;
                }catch (ApiRequestException apie) {
                    log.error("上传服务区信息失败！错误信息:"+apie.getMessage());

                    if(apie.getErrCode()>=700&&apie.getErrCode()<=800){
                        serviceAreaUploadDao.updateServiceAreaError(serviceArea.getId());
                    }
                    continue;
                }
            }
            return "上传服务区信息结束！";
        }else{
            return "无服务区信息,上传结束...";
        }
    }

    private String Double(double lat) {
        // TODO Auto-generated method stub
        return null;
    }

    //获取服务区信息
    @SuppressWarnings("unchecked")
    public List<tbl_ServiceArea> getServiceAreaList() throws IOException{
        List<tbl_ServiceArea> list;
        try{
            list = serviceAreaUploadDao.getServiceAreaList();
        }catch(Exception e){
            e.printStackTrace();
            log.error("获取服务区信息信息失败！错误信息:",e);
            throw new IOException("获取服务区信息失败:"+e.getMessage());
        }
        return list;
    }
}
